package com.leyou.item.web;


import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryBrandService categoryBrandService;

    /*分页条件查询*/
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key
    ) {
        return ResponseEntity.ok(this.brandService.pageQuery(page, rows, key));
    }

    /*新增品牌*/
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO) {
        this.brandService.addBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /*更新品牌*/
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {
        this.brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //根据品牌id集合查询品牌集合
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(
            @RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(this.brandService.queryBrandByIds(ids));
    }
   //根据集合id来查询品牌集合
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(
            @RequestParam("id")Long cid){
        return ResponseEntity.ok(this.categoryBrandService.queryBrandByCategory(cid));
    }

    //根据ID查询品牌
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long id){
        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }
    //根据ID来删除品牌
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteById(@PathVariable("id")Long bid){

        this.brandService.deleteById(bid);
        return ResponseEntity.ok().build();
    }

}


